var text = [
  {  searchPlaceHolder: '请输入搜索关键词',blank:'暂无历史搜索记录' },
  { searchPlaceHolder: 'please enter keyword', blank: 'empty history log' }
];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    hadHistory:true,
    historyList: wx.getStorageSync('historyList') || [],
    iconUrl: '/assets/module/',
  },
  onSearch(){
   this.onClick()
  },
  onChange(e){
    this.setData({
      value: e.detail
    });
  },
  /**
   * 清除历史缓存
   */
  cancleHistory(e){
    console.log(e)
    let del=e.currentTarget.dataset.index
    var tmp=this.data.historyList
    var setTmp=new Set(tmp)
    setTmp.delete(del)
    tmp=Array.from(setTmp)
    this.setData({
      historyList:tmp
    })
  },
  /**
   * 点击搜索后，向后端发送请求，同时往历史记录中添加缓存
   */
  onClick(){
    //todo 向后台发送请求
    //console.log('value:', this.data.value)
    var tmp=this.data.historyList
    tmp.push(this.data.value)
    //set array互转，去除重复的元素
    tmp = Array.from(new Set(tmp))
    this.setData({
      historyList:tmp
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let lang=wx.getStorageSync('language')
    this.setData({
      text: text[lang],
      theme: wx.theme,
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      historyList: wx.getStorageSync('historyList') || [],
    })

    //历史记录动画
    let array = [],
      endIndex = this.data.historyList.length;
    for (let i = 0; i < endIndex; ++i)
      array.push('searchHis[' + i + ']');
    wx._slide_v(this, array, wx.system.height * .8, 0, () => {
      wx.slide_v(this, array, 0, 1, 0);
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onUnload(){
   // console.log('unload')
    //console.log(this.data.historyList)
    wx.setStorageSync('historyList', this.data.historyList)
  }
})